package com.huifer.wgx.service.impl;

import com.huifer.wgx.utlis.DateUtil;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.model.ProjectEntity;
import com.huifer.wgx.repository.PersonnelRepository;
import com.huifer.wgx.repository.ProjectRepository;
import com.huifer.wgx.service.ProjectService;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * <p>Title : ProjectServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-19
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private PersonnelRepository personnelRepository;

    @Autowired
    private ProjectRepository projectRepository;


    /**
     * 创建项目
     *
     * @param projectName 项目名称
     * @param startTime 工程名称
     * @param projectScheduledTime 预计完成时间
     * @param personnelName 项目负责人
     * @return 项目
     */
    @Override
    public ProjectEntity saveProject(String projectName, Timestamp startTime,
            Integer projectScheduledTime, String personnelName) {
        try {

            PersonnelEntity byPersonnelName = personnelRepository
                    .findByPersonnelName(personnelName);
            ProjectEntity byProjectName = projectRepository.findByProjectName(projectName);

            if (byPersonnelName != null && byProjectName == null) {
                ProjectEntity projectEntity = new ProjectEntity();
                projectEntity.setPersonnelId(byPersonnelName.getPersonnelId());
                projectEntity.setProjectName(projectName);
                projectEntity.setProjectStartTime(startTime);
                projectEntity.setProjectScheduledTime(projectScheduledTime);
                ProjectEntity save = projectRepository.save(projectEntity);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更新项目结束时间
     *
     * @param projectName 项目名称
     * @param endTime 结束时间
     * @return 项目
     */
    @Override
    public ProjectEntity updateProject(String projectName, Timestamp endTime) {
        try {
            Specification<ProjectEntity> p = (Specification<ProjectEntity>) (root, criteriaQuery, criteriaBuilder) -> {
                Predicate pf1 = criteriaBuilder
                        .equal(root.get("projectName"), projectName);
                return pf1;
            };

            ProjectEntity editPro = projectRepository.findOne(p).get();
            if (editPro != null) {

                editPro.setProjectExecutionTime(
                        DateUtil.calcDays(editPro.getProjectStartTime(), endTime));

                editPro.setProjectEndTime(endTime);
                ProjectEntity save = projectRepository.save(editPro);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询所有项目
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 项目列表
     */
    @Override
    public Page<ProjectEntity> findAll(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "projectId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<ProjectEntity> all = projectRepository.findAll(pageable);
        return all;
    }

    /**
     * 查询所有
     *
     * @return 项目列表
     */
    @Override
    public List<ProjectEntity> list() {
        return projectRepository.findAll();
    }

}
